package com.example.demo.Controller;

import com.example.demo.dao.UserDao;
import com.example.demo.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
public class UserController {

    @Autowired
    private UserDao userDao ;

    @RequestMapping(value = "/add/{userName}/{passWord}")
    public String addUser(@PathVariable(value = "userName") String userName, @PathVariable("passWord") String passWord, HttpServletResponse response ){
        User user = new User();
        user.setUserName(userName);
        user.setPassWord(passWord);

        userDao.save(user);

        return "添加成功！！！";
    }

    @RequestMapping(value = "/delete/{id}")
    public String delete(@PathVariable(value = "id") Long id){

        User user = new User();
        user.setId(id);
        userDao.delete(user);

        return "删除成功！！！";
    }

    @RequestMapping(value = "/update/{id}/{userName}")
    public String update(@PathVariable(value = "id") Long id,@PathVariable(value = "userName") String userName){

        User user = userDao.getOne(id);
        user.setUserName(userName);
        userDao.save(user);

        return "修改成功！！！";
    }

    @RequestMapping(value = "/list")
    public List<User> List(){

        return userDao.findAll();
    }
}
